package com.web.controller.student;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.common.apiresult.CommonResult;
import com.common.domain.dto.WaterElectricityQueryDTO;
import com.service.serviceInterface.WaterElectricityService;
import com.service.utils.SecurityUtil;
import com.system.entity.WaterElectricity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;

/**
 * @author liuxuhuang
 * @date 2023年04月04日 10:48
 * @desc 宿舍水电费控制器
 */
@RestController(value = "studentWaterElectricityController")
@Api(value = "waterElectricity", tags = "宿舍水电费信息控制器")
@RequestMapping(value = "/student/waterElectricity")
@Slf4j
public class WaterElectricityController {
    @Resource
    private SecurityUtil securityUtil;
    @Resource
    private WaterElectricityService waterElectricityService;

    /**
     * userId  => studentId =>dormId
     * 通过宿舍id获取水电费信息
     *
     * @param dto
     * @return
     */
    @GetMapping
    @ApiOperation(value = "获取所在宿舍水电费信息")
    public CommonResult get(WaterElectricityQueryDTO dto) {
        long userId = securityUtil.getUserId(SecurityContextHolder.getContext());
        return waterElectricityService.selectPage(dto, userId);
    }

    /**
     * 缴费  ==>  水电费
     * 模拟缴费
     *
     * @param id
     * @return
     */
    @PutMapping
    @ApiOperation(value = "缴费.按月份缴费")
    public CommonResult pay(@RequestParam("id") @NotBlank(message = "参数不能为空") String id) {
        boolean update = waterElectricityService.update(new LambdaUpdateWrapper<WaterElectricity>().set(WaterElectricity::getIsPay, 1).eq(WaterElectricity::getId, id));
        return update ? CommonResult.success() : CommonResult.failed();
    }
}
